package com.person.service.impl;

import cn.hutool.core.util.IdUtil;
import com.person.common.BaseContext;
import com.person.dto.DishDto;
import com.person.dto.SetmealDto;
import com.person.mapper.SetmealDishMapper;
import com.person.pojo.SetmealDish;
import com.person.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
@Slf4j
@Service
public class SetmealDishServiceImpl implements SetmealDishService {
    @Autowired
    private SetmealDishMapper setmealdishmapper;
    @Override
    public void add(SetmealDto setmealDto) {
        //套餐id，查询setmeal_dish表中是否有，有则删除
        Long setmealId= setmealDto.getId();
        if (setmealId!=null){
            setmealdishmapper.remove(setmealId);
        }else{
            log.info("此套餐没有菜品");
        }

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        log.info("套餐中添加的菜品:{}",setmealDishes);
        //判断套餐菜品是否为空
        if (setmealDishes!=null){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setId(IdUtil.getSnowflakeNextId());
                setmealDish.setSetmealId(setmealDto.getId());
                setmealDish.setCreateTime(LocalDateTime.now());
                setmealDish.setUpdateTime(LocalDateTime.now());
                setmealDish.setCreateUser(BaseContext.getCurrentId());
                setmealDish.setUpdateUser(BaseContext.getCurrentId());
            }
            setmealdishmapper.add(setmealDishes);
        }
    }

    @Override
    public List<SetmealDish> inquire(Long id) {
        List<SetmealDish> setmealDishes = setmealdishmapper.select(id);
        return setmealDishes;
    }


}
